package com.aaa.controller;

import com.aaa.entity.ExamEntity;
import com.aaa.entity.Message;
import com.aaa.entity.Pager;
import com.aaa.entity.ScoreEntity;
import com.aaa.service.ExamService;
import com.aaa.service.ExamServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet(name = "ExamServlet",urlPatterns ="/exam.action" )
public class ExamServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String method=request.getParameter("method");
        if (method!=null) {
            switch (method) {
                case "queryAll":
                    queryAll(request, response);
                    break;
                case "add":
                    add(request,response);
                    break;
                case "queryOne":
                    queryOne(request,response);
                    break;
                case "delete":
                    delete(request,response);
                    break;
                case "updateState":
                    updateState(request,response);
                    break;
                case "queryClass":
                    queryClass(request,response);
                    break;
                case "queryStu":
                    queryStu(request,response);
                    break;
                case "add_score":
                    add_score(request,response);
                    break;
            }
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         doPost(request,response);
    }

    protected void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sPageNo=request.getParameter("pageNo");
        if (sPageNo==null) sPageNo="1";
        int pageNo= Integer.parseInt(sPageNo);
       ExamService service=new ExamServiceImpl();
        Pager pager=service.queryAll(pageNo);
       String str= JSON.toJSONStringWithDateFormat(pager,"yyyy-MM-dd");
       response.getWriter().print(str);
    }

    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String exam_id=request.getParameter("id");
        int Id=exam_id==""?0: Integer.parseInt(exam_id);
        String exam_name=request.getParameter("exam_name");
        String date=request.getParameter("date");
        ExamEntity examEntity=new ExamEntity();
        examEntity.setId(Id);
        examEntity.setExamName(exam_name);
        examEntity.setDate(java.sql.Date.valueOf(date));
        Message message=new Message();

        ExamService service=new ExamServiceImpl();
        message=service.Update(examEntity);
        String str=JSON.toJSONStringWithDateFormat(message,"yyyy-MM-dd");
        response.getWriter().print(str);
    }

    protected void queryOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
       String id=request.getParameter("id");
       int exam_id= Integer.parseInt(id);
        ExamService service=new ExamServiceImpl();
       Map map=service.queryOne(exam_id);
       String str=JSON.toJSONStringWithDateFormat(map,"yyyy-MM-dd");
       response.getWriter().print(str);
    }

    protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id=request.getParameter("id");
        int exam_id= Integer.parseInt(id);
        ExamService service=new ExamServiceImpl();
        int result=service.delete(exam_id);
        response.getWriter().print(result);

    }


    protected void updateState(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id=request.getParameter("id");
        int exam_id= Integer.parseInt(id);
        String state=request.getParameter("state");
        int exam_state= Integer.parseInt(state);
        ExamService service=new ExamServiceImpl();
        int result=service.updateState(exam_id,exam_state);
        response.getWriter().print(result);

    }

    protected void queryClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

        ExamService service=new ExamServiceImpl();
        List list= service.queryClass();
        String str=JSON.toJSONStringWithDateFormat(list,"yyyy-MM-dd");
        response.getWriter().print(str);

    }

    protected void queryStu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String classid=request.getParameter("classid");
        int class_id= Integer.parseInt(classid);
        ExamService service=new ExamServiceImpl();
        List list=service.queryStu(class_id);
        String str=JSON.toJSONStringWithDateFormat(list,"yyyy-MM-dd");
        response.getWriter().print(str);
    }

    protected void add_score(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String classid=request.getParameter("class_id");
        int class_id= Integer.parseInt(classid);
        String examid=request.getParameter("exam_id");
        int exam_id= Integer.parseInt(examid);
        //获取一组学号
        String[] stuid=request.getParameterValues("stuid");
        String[] stuname=request.getParameterValues("stuname");
        //获取一组成绩
        String[] score=request.getParameterValues("score");
        List <ScoreEntity> scoreList=new ArrayList<>();
        for (int i = 0; i < stuid.length; i++) {
           int score1=score[i]==""?0:Integer.parseInt(score[i]);
           ScoreEntity scoreEntity=new ScoreEntity(Integer.parseInt(stuid[i]),exam_id,score1,class_id,stuname[i]);
           scoreList.add(scoreEntity);
        }
        ExamService service=new ExamServiceImpl();
        int a =service.addScore(scoreList);
        response.getWriter().print(a);

    }
}
